package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.AddressItem;
import com.zadtek.shopmall.bean.BankCardItem;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.SpacesItemDecoration;

import java.util.LinkedList;
import java.util.List;

public class BankCardListActivity extends AbsActivity {
    private ImageView ivBack;
    private TextView topTitle,tvAddCard;
    private RecyclerView recyclerView;
    private List<BankCardItem> data = new LinkedList<>();
    private BankCardListAdapter adapter;
    private int page =1;
    private boolean canLoadMore = true;
    @Override
    public int setView() {
        return R.layout.activity_address_list;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        topTitle = (TextView) findViewById(R.id.title);
        topTitle.setText("银行卡信息");
        tvAddCard = (TextView) findViewById(R.id.tv_add_address);
        tvAddCard.setText("新增银行卡");
        recyclerView = (RecyclerView)findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(getResources().getDimensionPixelSize(R.dimen.little_margin)));
    }

    private void initLogic(){
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvAddCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addNewBankCard();
            }
        });
        adapter = new BankCardListAdapter(R.layout.bankcard_item,data);
        recyclerView.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                requestData();
            }
        },recyclerView);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.iv_delete:
                        ToastUtil.showToast("delete");
                        break;
                }
            }
        });
    }

    private void requestData() {
        if(!canLoadMore){
            return;
        }
        BankCardItem item = new BankCardItem();
        data.add(item);
        adapter.notifyDataSetChanged();
        page++;
        if(page>10){
            canLoadMore = false;
            adapter.loadMoreEnd();
        }else{
            adapter.loadMoreComplete();
        }

    }


    private void addNewBankCard() {
        startActivity(new Intent(this,AddNewCardActivity.class));
        ToastUtil.showToast("添加新银行卡请求！");
    }

    private class BankCardListAdapter extends BaseQuickAdapter<BankCardItem,BaseViewHolder> {
        public BankCardListAdapter(int layoutResId, @Nullable List<BankCardItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BankCardItem item) {
            helper.addOnClickListener(R.id.iv_delete);
        }
    }
}
